package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

//网站无效
public class BianewsNewsAnalyse implements AnalyseNews{

	private static final String[] regex={
				"http://www.bianews.com/news/[0-9]*/n-[0-9]*.html",
				"http://www.bianews.com/viewnews-[0-9]*.html"};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}

			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}



			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();

				String title=null;
				String content=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				title=doc.select("div.article-title").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.article-box"));
				String text=doc.select("div.hot-foot").text();
				String author=null;
				author=JSoupUtils.matchAuthor(doc, "来源：");
				date=DateUtils.matchDate(text);
				news.setAuthor(author);
				news.setUrl(url);
				news.setTitle(title);
				news.setContent(content);
				news.setDate(date);
				return news;
			}

			public static void main(String[] args) {
				BianewsNewsAnalyse test=new BianewsNewsAnalyse();

				String url="http://www.bianews.com/news/50/n-430250.html";
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
